import java.util.PriorityQueue;
import java.util.Stack;

public class LeetCode {
    public static void main(String[] args) {
        String s = "100[leetcode]";
        String ss = decodeString(s);
    }
    public static String decodeString(String s) {

        Stack<StringBuffer> str = new Stack<>();
        str.push(new StringBuffer());
        Stack<Integer> cou = new Stack<>();
        int i = 0;
        while(i < s.length()){
            if(s.charAt(i)>='0'&&s.charAt(i)<='9'){
                int tmp = 0;
                while(s.charAt(i)>='0'&&s.charAt(i)<='9'){
                    tmp = tmp*10 + (s.charAt(i)-'0');
                    i++;
                }
                cou.push(tmp);
            }else if(s.charAt(i)=='['){
                i++;
                StringBuffer tmp = new StringBuffer();
                while(s.charAt(i)>='a'&&s.charAt(i)<='z'){
                    tmp.append(s.charAt(i));
                    i++;
                }
                str.push(tmp);
            }else if(s.charAt(i) == ']'){
                StringBuffer tmp = new StringBuffer(str.peek());
                StringBuffer tmp1 = new StringBuffer(str.pop());
                int count = cou.pop();
                for (int j = 1; j < count; j++) {
                    tmp.append(tmp1);
                }
                str.peek().append(tmp);
                i++;
            }else{
                str.peek().append(s.charAt(i));
                i++;
            }
        }
        return str.peek().toString();
    }
}
